package com.gxa.p2p.service.impl;

import com.gxa.p2p.mapper.LoginLogMapper;
import com.gxa.p2p.pojo.LoginLog;
import com.gxa.p2p.service.LoginAndLogoutInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 用户登录信息查询接口实现类
 *
 * @author Longlin_Feng
 * @date 2020/8/1
 */
@Service
public class LoginAndLogoutInfoServiceImpl implements LoginAndLogoutInfoService {

  @Resource private LoginLogMapper loginLogMapper;

  /**
   * 查询所有历史记录
   *
   * @param userId 当前系统用户id
   * @return List<LoginLog>
   */
  @Override
  public List<LoginLog> findLoginHistory(Integer userId) {

    return loginLogMapper.selectByUserId(userId);
  }

  /**
   * 登出操作
   *
   * @return boolean登出成功/失败(记录时间)
   */
  @Override
  @Transactional(
      propagation = Propagation.REQUIRED,
      isolation = Isolation.READ_COMMITTED,
      rollbackFor = {Throwable.class})
  public boolean logout(Integer id) {

    int lines = loginLogMapper.updateLogoutDateByLoginDate(new Date(), id);
    return lines > 0;
  }
}
